package com.validated.constraint;

import com.validated.annotation.Phone;
import com.validated.util.ObjectUtil;
import java.util.regex.Pattern;

/**
 * 手机号码校验
 *
 * @author Moo
 * @date 2023/05/11
 */
public class PhoneConstraint implements ConstraintValidator<Phone,Object> {
    @Override
    public boolean isValid(Phone phone, Object objectValue, Class<?> valueType) {
        if(phone.enableNull() && null == objectValue){
            return true;
        }
        String regexp = phone.regexp();
        String value = objectValue == null ? "" : objectValue.toString();

        if (ObjectUtil.isEmpty(value)) {
            return false;
        } else {
            Pattern pattern = Pattern.compile(regexp);
            return pattern.matcher(value).matches();
        }
    }

}
